﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Microsoft.Practices.Unity;
namespace Core.Presenters
{
    class JuridicalPersonPresenter
    {
        private IJuridicalPersonDAO _juridicalPersonDao;
        private IJuridicalPerson _juridicalPerson;
        private IJuridicalPersonView _view;

        public JuridicalPersonPresenter(IJuridicalPersonView view,IJuridicalPerson juridicalPerson = null)
        {
            
            _view = view;
            _juridicalPersonDao = ResolverContainer.Resolver.Resolve<IJuridicalPersonDAO>();
            _juridicalPerson = juridicalPerson ?? ResolverContainer.Resolver.Resolve<IJuridicalPerson>(); 
            _view.JuridicalPerson.DataSource = _juridicalPerson;
            _juridicalPerson.ToJuragial();
            _view.SaveCustomer += Save;
            _view.ShowDialog();
        }

        private void Save(object sender, EventArgs eventArgs)
        {
            if (_juridicalPersonDao.SaveOrUpdateAndCommit((IDomainObject) _view.JuridicalPerson.Current))
            {
               _view.Close();
            }
        }
    }
}
